package com.xiaobai.frame.service.impl;

import com.xiaobai.common.utils.config.Tree.TreeHelper;
import com.xiaobai.common.utils.result.R;
import com.xiaobai.common.utils.result.Result;
import com.xiaobai.common.utils.result.ResultCode;
import com.xiaobai.common.utils.tool.Func;
import com.xiaobai.common.utils.tool.Safes;
import com.xiaobai.data.manager.system.MenuManager;
import com.xiaobai.data.manager.system.RoleMenuManager;
import com.xiaobai.frame.service.IMenuService;
import com.xiaobai.model.convertor.system.MenuConvertor;
import com.xiaobai.model.entity.system.Menu;
import com.xiaobai.model.entity.system.RoleMenu;
import com.xiaobai.model.vo.system.menu.MenuAddVO;
import com.xiaobai.model.vo.system.menu.MenuTreeVO;
import com.xiaobai.model.vo.system.menu.MenuUpdateVO;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

import static com.xiaobai.common.utils.tool.CommonConstants.ROOT_ID;
@Service
@AllArgsConstructor
public class MenuServiceImpl implements IMenuService {

    private final MenuManager menuManager;
    private final RoleMenuManager roleMenuManager;


    @Override
    public Result getMenuTree(Long id) {
        if (id == null) id = ROOT_ID;
        List<Menu> menuList = menuManager.list();
        List<MenuTreeVO> treeVOList = MenuConvertor.INSTANCE.toTreeVOList(menuList);
        TreeHelper<Long, MenuTreeVO> helper = new TreeHelper<>(treeVOList, ROOT_ID, id);
        return R.success(helper.create());
    }

    @Override
    public Result add(MenuAddVO menuAddVO) {
        if (Func.isNotBlank(menuAddVO.getMenuName())) {
            Menu menu = menuManager.getMenuByAuthority(menuAddVO.getMenuName());
            if (Func.isNotEmpty(menu)) return R.fail(ResultCode.SAME_RESOURCES, "已有该权限数据");
        }
        Menu entity = MenuConvertor.INSTANCE.toAddEntity(menuAddVO);
        boolean save = menuManager.save(entity);
        return R.success(save);
    }

    @Override
    public Result update(MenuUpdateVO menuUpdateVO) {

        Menu menu = menuManager.getById(menuUpdateVO.getId());
        if (menu == null) return R.fail(ResultCode.NOT_SEEK_RESOURCE, "未查询到对应数据,请重试");

        Menu menu1 = menuManager.getMenuByIdAuthority(menuUpdateVO.getId(),menuUpdateVO.getAuthority());
        if (Func.isNotEmpty(menu1)) return R.fail(ResultCode.SAME_RESOURCES, "已有该权限数据");

        Menu entity = MenuConvertor.INSTANCE.toUpdateEntity(menuUpdateVO);

        boolean update = menuManager.updateById(entity);
        return R.success(update);
    }

    @Override
    public Result delete(List<Long> ids) {
        //删除和角色之间的关联
        boolean delete = roleMenuManager.deleteMenuIds(ids);
        //删除菜单表
        boolean b = menuManager.removeBatchByIds(ids);

        return R.success(b && delete);
    }

    @Override
    public Result getRoleMenu(Long role) {
        List<RoleMenu> roleMenuList = roleMenuManager.getListByRoleId(role);
        List<Long> menuIds = Safes.of(roleMenuList).stream().map(RoleMenu::getMenuId).collect(Collectors.toList());
        List<Menu> menuList = menuManager.getMenuTree();
        List<MenuTreeVO> treeVOList = MenuConvertor.INSTANCE.toTreeVOList(menuList);
        treeVOList.forEach(menuTreeVO -> menuTreeVO.setIsAuthority(menuIds.contains(menuTreeVO.getId())));
        TreeHelper<Long, MenuTreeVO> helper = new TreeHelper<>(treeVOList, ROOT_ID);
        return R.success(helper.create());
    }

    @Override
    public Result get(Long id) {
        return R.success(menuManager.getOptById(id));
    }
}
